package com.jdk.demo.mappedByteBuffer;

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class Test16_3 {
	
	// 专用模式
	public static void main(String[] args) throws Exception {
		File file = new File("n3.txt");
		RandomAccessFile fileA = new RandomAccessFile(file, "rw");
		FileChannel fileChannelA = fileA.getChannel();
		MappedByteBuffer buffer = fileChannelA.map(FileChannel.MapMode.PRIVATE, 0, 5);
		System.out.println((char)buffer.get() + " position=" + buffer.position()); // a
		System.out.println((char)buffer.get() + " position=" + buffer.position()); // b
		System.out.println((char)buffer.get() + " position=" + buffer.position()); // c
		System.out.println((char)buffer.get() + " position=" + buffer.position()); // d
		System.out.println((char)buffer.get() + " position=" + buffer.position()); // e
		
		buffer.position(0);
		buffer.put((byte)'o');
		buffer.put((byte)'p');
		buffer.put((byte)'q');
		buffer.put((byte)'r');
		buffer.put((byte)'s');
		
		// 文件内容还是"abcde"
		fileA.close();
		fileChannelA.close();
	}

}
